import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:study_demo/ui/widgets_example/drag_grid_view/drag_grid_view_controller.dart';

class DragGridViewPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<DragGridViewController>(
        init: DragGridViewController(),
        builder: (controller) {
          return Scaffold(
              appBar: AppBar(title: Text('可拖动排序GridView')),
              body: GridView.builder(
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 5,
                  childAspectRatio: 1.0,
                  mainAxisSpacing: 8,
                  crossAxisSpacing: 8,
                ),
                padding: EdgeInsets.all(8),
                itemCount: controller.items.length,
                itemBuilder: (context, index) {
                  return GestureDetector(
                    onTapDown: (_) => controller.setPressed(index, true),
                    onTapUp: (_) => controller.setPressed(index, false),
                    onTapCancel: () => controller.setPressed(index, false),
                    child: LongPressDraggable<int>(
                      data: index,
                      feedback: Material(
                        elevation: 4,
                        child: Container(
                          width: 100,
                          height: 100,
                          decoration: BoxDecoration(
                            color: Colors.redAccent.withValues(alpha: 0.9),
                            // borderRadius: BorderRadius.circular(8),
                          ),
                          child: Center(
                            child: Text(
                              controller.items[index],
                              style:
                                  TextStyle(color: Colors.white, fontSize: 18),
                            ),
                          ),
                        ),
                      ),
                      childWhenDragging: Opacity(
                        opacity: 0.5,
                        child: _buildItem(controller.items[index]),
                      ),
                      onDragStarted: () => controller.startDrag(index),
                      onDragEnd: (_) => controller.endDrag(),
                      child: DragTarget<int>(
                        onWillAccept: (data) => data != index,
                        onAccept: (fromIndex) =>
                            controller.swapItems(fromIndex, index),
                        builder: (context, candidateData, rejectedData) {
                          return _buildItem(
                            controller.items[index],
                            isDraggedOver: candidateData.isNotEmpty,
                            isDragging: controller.draggedIndex.value == index,
                            isPressed: controller.pressedIndex.value == index &&
                                controller.isPressed.value,
                          );
                        },
                      ),
                    ),
                  );
                },
              ));
        });
  }

  Widget _buildItem(
    String text, {
    bool isDraggedOver = false,
    bool isDragging = false,
    bool isPressed = false,
  }) {
    return Opacity(
      opacity: isPressed ? 0.9 : 1.0,
      child: Container(
        decoration: BoxDecoration(
          color: isDragging
              ? Colors.redAccent.withOpacity(0.7)
              : isDraggedOver
                  ? Colors.redAccent[200]
                  : Colors.redAccent,
          borderRadius: BorderRadius.circular(8),
          border:
              isDraggedOver ? Border.all(color: Colors.white, width: 2) : null,
        ),
        child: Center(
          child: Text(
            text,
            style: TextStyle(
              color: Colors.white,
              fontSize: 18,
              fontWeight: FontWeight.bold,
            ),
          ),
        ),
      ),
    );
  }
}
